package com.groupbuying.storeinternal.controller.forInternal;

import com.groupbuying.storeinternal.controller.forManage.BaseController;
import com.groupbuying.storeinternal.service.StoreOpexRuleRelPreService;
import com.groupbuying.storeinternal.service.StoreOpexRuleRelService;
import com.groupbuying.storeinternal.utils.ParamUtils;
import com.groupbuying.storeinternal.utils.R;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Api("服务商运营规则编辑")
@RequestMapping("/storeOpexRule")
@RestController
public class StoreOpexRuleController extends BaseController {
	@Autowired
	private StoreOpexRuleRelPreService storeOpexRuleRelPreService;

	@Autowired
	private StoreOpexRuleRelService storeOpexRuleRelService;
	/**
	 * 获取门店预设运营规则
	 * @author zmlmagic
	 */
    @PostMapping("/getOpexList")
    public R getOpexList(@RequestBody Map<String, Object> params) {
    	return storeOpexRuleRelPreService.getOpexRuleList(params);
    }

	/**
	 * 编辑门店预设运营规则
	 * @author zmlmagic
	 */
	@PostMapping("/editOpex")
	public R editOpex(@RequestBody Map<String, Object> params) {
		return storeOpexRuleRelPreService.editOpexRule(params);
	}

	/**
	 * 更新指定门店的预表到规则表
	 * @author zmlmagic
	 */
	@PostMapping("/pushOpex")
	public R pushOpex(HttpServletRequest request) {
		Map<String, Object> mapParams = ParamUtils.getParamMap(request);
		Object temp = mapParams.get("storeUuid");
		String storeUuid = temp != null ? temp.toString() : null;
		temp = mapParams.get("password");
		String password = temp != null ? temp.toString() : null;
		if (StringUtils.isEmpty(storeUuid) || StringUtils.isEmpty(password) || !password.equals("115915250")) {
			return R.paramIsNull();
		}
		return storeOpexRuleRelService.pushPreOpexToMaster(storeUuid);
	}

	/**
	 * 每晚定时更新门店优惠规则
	 * @author zmlmagic
	 */
	@PostMapping("/batchPushOpex")
	public R batchPushOpex(@RequestBody Map<String, Object> params) {
		Object temp = params.get("password");
		String password = temp != null ? temp.toString() : null;
		if (StringUtils.isEmpty(password) || !password.equals("115915250")) {
			return R.paramIsNull();
		}
		return storeOpexRuleRelService.batchPushPreOpexToMaster();
	}

}
